/**
 * 
 */
package dms.repository.impl;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import dms.model.Campus;
import dms.model.util.PageIterator;
import dms.model.util.FindUtil;
import dms.repository.CampusRepository;
import dms.util.HibernateUtil;

/**
 * @author 李健
 *
 */
public class CampusRepositoryHibernate implements CampusRepository {
	
	public PageIterator<Campus> findAll(int start, int count) {
		Session session = null;
		Transaction tx = null;
		
		List<Campus> campusList = null;
		PageIterator<Campus> iter = null;
		Long allCount = null;
		
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			
			String findQuery = "select c from Campus c";
			String countQuery = "select count(*) from Campus c";
			allCount = FindUtil.calculateAllCount(session, countQuery, null);
			campusList = FindUtil.getEntitiesByPage(session, findQuery, null, start, count);
			
			iter = new PageIterator<Campus>(campusList, allCount, start, count);
			tx.commit();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
		
		return iter;
	}

	public Campus findById(Long id) {
		Session session = null;
		Transaction tx = null;
		Campus campus = null;
		
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			
			Query query = session.createQuery("select c from Campus c where c.id = :id");
			query.setLong("id", id);
			campus = (Campus) query.uniqueResult();
			
			tx.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
		
		return campus;
	}

	public Campus saveCampus(Campus campusInfo) {
		Session session = null;
		Transaction tx = null;
		
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			
			session.save(campusInfo);
			
			tx.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
		
		return campusInfo;
	}
	
	public Campus updateCampus(Campus campusInfo) {
		Session session = null;
		Transaction tx = null;
		Campus c = null;
		
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			
			c = (Campus) session.merge(campusInfo);
			
			tx.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
		
		return c;
	}
	/**
	 * 通过load上来之后再delete
	 * @param id
	 */
	
	public void deleteCampusById(Long id) {
		Session session = null;
		Transaction tx = null;
		Campus c = null;
		
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			
			c = (Campus) session.load(Campus.class, id);
			session.delete(c);				
			
			tx.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	@Override
	public void deleteCampuses(List<Long> ids) {
		Session session = null;
		Transaction tx = null;

		String deleteQuery = "delete from Campus c where c.id in (:ids)";
		
		try {
			session = HibernateUtil.getSessionFactory().openSession();
			tx = session.beginTransaction();
			
			session.createQuery(deleteQuery)
				   .setParameterList("ids", ids)
				   .executeUpdate();
			
			tx.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (session != null) {
				if (session.isOpen()) {
					session.close();
				}
			}
		}
		
	}
}
